/*
 * Copyright (c) 2006-2018, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2018-09-14     SummerGift   Add spi flash example
 */

#include <rtthread.h>
#include <fal.h>
#include <dfs_fs.h>
#include <board.h>
#include "key.h"
#include "pms.h"

#define LOG_LVL     LOG_LVL_DBG
#define LOG_TAG     "main"
#include <ulog.h>

#define FS_PARTITION_NAME  "filesystem"

extern int ulog_file_backend_init(void);

void mount_to_usb(void)
{
    char *fullpath = NULL;

    fullpath = dfs_normalize_path(NULL, "/");
    if (dfs_unmount(fullpath) == 0)
    {
        rt_kprintf("mount_to_usb ok!\n");
    }
    else
    {
        rt_kprintf("mount_to_usb fail!\n");
    }
}

void mount_to_flash(void)
{
    rt_device_t dev;

    dev = rt_device_find(BLK_DEV_NAME);
    if (dev != RT_NULL && dev->open_flag != 0)
    {
        rt_device_close(dev);
        rt_kprintf("close usb device ok!\n");
    }

    if (dfs_mount(BLK_DEV_NAME, "/", "elm", 0, 0) == 0)
    {
        rt_kprintf("mount_to_flash ok!\n");
    }
    else
    {
        rt_kprintf("mount_to_flash error!!\n");
    }
}

int main(void)
{
    fal_init();
    key_gpio_init();
    ulog_file_backend_init();
    pms_log_timer_init();
    pms_log_timer_start();
    LOG_D("fal_init ok!\n");
    return 0;
}

void log_console_test(void)
{
    LOG_D("log_console_test ok!");
}

MSH_CMD_EXPORT(log_console_test, log_console_test);
MSH_CMD_EXPORT(mount_to_usb, mount_to_usb);
MSH_CMD_EXPORT(mount_to_flash, mount_to_flash);
